#include "MATSSVEP.h"
#include "SSVEP_Dialog.h"


Matlab_SSVEP::Matlab_SSVEP(void): MatAlgorithm()
{
	wxString Temp;
	wxFileDialog Dialog(NULL);
	Open(768,768,8,4,1);
	EvalString(_("Dati = [];Out = zeros(4,1);"));
	SSVEP_Dialog	Dia;
	Dia.ShowModal();

	SetPeriods(Dia.SelPeriods);
	Temp = wxString::Format(_("Periods = [%d %d %d %d];"),Dia.SelPeriods[0],Dia.SelPeriods[1],Dia.SelPeriods[2],Dia.SelPeriods[3]);
	//pMatlab->EvalString("[bh,ah]=butter(5,5/128,'high');");
	EvalString(Temp);
	EnableFeedback = Dia.m_SendFeed->IsChecked();
	EnableClassification = Dia.m_SendClass->IsChecked();
	Filter.LoadFromFile(_T("./filters/SSVEP_Band5-40_5.f"));

	memcpy(Ref,&Dia.Refs[0],sizeof(Ref));
	memcpy(Sig,&Dia.Sigs[0],sizeof(Sig));
	RefTot = SigTot = 0;
	for(int i=0;i<8;i++)
	{
		if(Sig[i])SigTot++;
		if(Ref[i])RefTot++;
	}
	if(EnableClassification)
	{
		wxString Name;
		Dialog.SetWildcard(_T("XDA Training files   (*.xda)|*.xda"));
		if(wxID_OK == Dialog.ShowModal())
		{
			//Name = Dialog.GetPath();
			//sprintf(Temp,"T = importdata('%s');",Name.ToAscii());
			Name = wxString(_("T = importdata('"))+Dialog.GetPath()+wxString(_("');"));
			EvalString(Name);
			EvalString(wxString(_("RES=T.RES;")));
			Error = false;
		}
		else
		{
			wxMessageBox(_("Error, you have not selected the XDA file."), _("Error"));
			Error=true;
		}
	}
	else
		Error = false;
	SingleSample = false;
}

Matlab_SSVEP::~Matlab_SSVEP(void)
{
}

void Matlab_SSVEP::SetPeriods(int *pPeriods)
{
	//Inserire la parte di matlab
	memcpy(&Periods[0],pPeriods,sizeof(Periods));	
}
void Matlab_SSVEP::SendPeriods(void)
{
	//Inserire la parte di matlab

	BCIMessage Temp;
	Temp = ComposeMessage(SET_PARAMS,0,Periods,sizeof(Periods));
	WriteOut((char*)&Temp,sizeof(Temp));		
	
}
void Matlab_SSVEP::PutSample(SampleStruct *pData)
{
	SampleStruct LocalData = *pData;
	float Reference = 0,Signal = 0;
	for(int i=0;i<8;i++)
	{
		if(Sig[i])Signal += LocalData.Data.Signal[i];
		if(Ref[i])Reference += LocalData.Data.Signal[i];
			
	}
	
	LocalData.Data.Signal[0] =Signal/SigTot;
	if(RefTot != 0) LocalData.Data.Signal[0] -= Reference/RefTot;
	Filter.FilterSample(&LocalData.Data);
	//Mettere qua se serve filtro spaziale
	Algorithm::PutSample(&LocalData);
	pData->Algo = LocalData.Algo;

}
void Matlab_SSVEP::RunSecWin(void)
{
	c4mMatrix	Out;
	float tFeed[4];
	Out.cols = 4;
	Out.rows = 1;
	Out.pdata = &tFeed;
	Out.esize = sizeof(float32);
	
	c4mMatrix	Class;
	float32 dClass;
	Class.cols = Class.rows = 1;
	Class.pdata = &dClass;
	Class.esize = sizeof(float32);
	

	//Inserisco i dati
	PutIntoMatlab("FeedWin",c4mSecWindow);
	ShiftSecondaryWin(64);

	//Elaboro i dati
	EvalString(_("Feature = single(MeanFeed256(FeedWin(:,1)',Periods));"));
	EvalString(_("Feedback = single(100*MeanFeed256_BaseRem(FeedWin(:,1)',Periods));"));
	GetFromMatlab("Feedback",&Out);
	SetFeedback(tFeed);
	if(EnableClassification)
	{
		EvalString(_("[Class, Tsd] = ApplyXda(Feature,RES);"));
		GetFromMatlab("Class",&Class);
		if(dClass != 0)
		{
			SetClassification(dClass);
		}
	}
	
	
	//pMatlab->EvalString("len = length(Dati)-1000; if(len<1)len = 1;end;plot(Dati(:,len:end)');");
}

void Matlab_SSVEP::RunMainWin(void)
{

	ShiftMainWin(768);
/*
	pMatlab->EvalString("[Class, Tsd] = single(ApplyXda(Feedback,RES);)");
	GetFromMatlab("Out",&Class);
	if(dClass != 0)
	{
		SetClassification(dClass);
	}*/
}



